
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;

namespace smasb2b
{
	public class VNDW0012 : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected ddscControlLibrary.InputBoxCtrl txtVENDOR_NO;
		protected ddscControlLibrary.InputBoxCtrl txtE_MAIL;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.WebControls.Label lblMSG004;
		protected System.Web.UI.WebControls.Label lblVENDOR_NO;
		protected System.Web.UI.WebControls.Label lblEMAIL;
		protected System.Web.UI.WebControls.Label lblMSG005;
		protected System.Web.UI.WebControls.Label lblMSG006;
		protected System.Web.UI.WebControls.ImageButton btnSUBMIT;
		protected ddscControlLibrary.SubmitButtonCtrl btnQuery;
		protected System.Web.UI.WebControls.Label lblVENDOR_NO_ERR;
		protected System.Web.UI.WebControls.Label lblE_MAIL_ERR;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		//Dim BasePage As BasePage = New BasePage
		DataBase DataBase = new DataBase();
		BasePage BasePage = new BasePage();
		StringBuilder sb = new StringBuilder();
		VENDOR VENDOR;
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			this.btnQuery.Click+=btnQuery_Click;
		}
		
		public DataSet Query_VENDOR_NO(com.ddsc.common.ddscDataSet p_ddscDt)
		{
			DataSet dsReturn = default(DataSet);
			DataSet ds = p_ddscDt.getDataSet();
			string VENDOR_NO = "";

            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
			try
			{
				
				// 查詢資料總筆數之 sql command
				StringBuilder sb = new StringBuilder();
				VENDOR_NO = ds.Tables[0].Rows[1]["VENDOR_NO"].ToString();
				sb.Append("Select * From V_VND_VENDOR ");
				//sb.Append("Where PK_VENDOR_NO = \'" + VENDOR_NO + "\'");
                sb.Append("Where PK_VENDOR_NO = :VENDORNO");
                m_arrParam.Add(dba.GetOracleParam(":VENDORNO", VENDOR_NO));

				// DataTable Name
				string dtName = "VND_B2B_VENDOR";
				
				// Sql command
				string sqlCommands = sb.ToString();
				
				// create database access object
				//dbAccess dba = new dbAccess();
				//dsReturn = dba.runExecuteQuery(dtName, sqlCommands);
                dsReturn = dba.runExecuteQuery(dtName, sqlCommands, m_arrParam);
			}
			catch (Exception ex)
			{
				// create return object (DataSet)
				dsReturn = new DataSet("V_VND_VENDOR");
				dsReturn.Tables.Add(createErrorTable(ex.Message));
				
				
				
			}
			
			return dsReturn;
		}
		
		private void Query_PW()
		{
			// 設定傳送的 DataSet 物件
			com.ddsc.common.ddscDataSet ddscDs = new com.ddsc.common.ddscDataSet();
			ResourceManager rm = BasePage.GetResourceManager();
			//Dim LoginUser As VENDOR = GetLoginUser()
			
			// 宣告回傳DataSet
			DataSet dsResult = new DataSet();
            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
			try
			{
				FunctionHandler FunctionHandler = new FunctionHandler();
				string PW = "";
				PW = System.Convert.ToString(FunctionHandler.GetRndString(8));
				
				string PASSWORD = FunctionHandler.Encrypt(PW);
				com.ddsc.common.ddscDataTable ddscDtp = ddscDs.createDataTable("VND_VENDOR_AUTHORITY");
				com.ddsc.common.ddscDataTable ddscDtm = ddscDs.createDataTable("VND_B2B_VENDOR");
				if (string.IsNullOrEmpty(VENDOR.PW_STATUS))
				{
					ddscDtp.addNewRow();
					ddscDtp.setColumnValue("action", "string", "add");
					ddscDtp.setColumnValue("VENDOR_NO", "string", this.txtVENDOR_NO.Text);
					ddscDtp.setColumnValue("VENDOR_PWD", "string", PASSWORD);
					ddscDtp.setColumnValue("status", "string", "0");
					ddscDtp.setColumnValue("crt_id", "string", this.txtVENDOR_NO.Text);
					ddscDtp.setColumnValue("crt_date", "string", SystemDate);
					ddscDtp.setColumnValue("crt_time", "string", SystemTime);
					ddscDtp.setColumnValue("UPD_id", "string", this.txtVENDOR_NO.Text);
					ddscDtp.setColumnValue("UPD_date", "string", SystemDate);
					ddscDtp.setColumnValue("UPD_time", "string", SystemTime);
					
					
					ddscDs.addDataTable(ddscDtp);
					ddscDtm.addNewRow();
					ddscDtm.setColumnValue("action", "string", "add");
					ddscDtm.setColumnValue("VENDOR_NO", "string", this.txtVENDOR_NO.Text);
					ddscDtm.setColumnValue("status", "string", "0");
					ddscDtm.setColumnValue("crt_id", "string", this.txtVENDOR_NO.Text);
					ddscDtm.setColumnValue("crt_date", "string", SystemDate);
					ddscDtm.setColumnValue("crt_time", "string", SystemTime);
					ddscDtm.setColumnValue("UPD_id", "string", this.txtVENDOR_NO.Text);
					ddscDtm.setColumnValue("UPD_date", "string", SystemDate);
					ddscDtm.setColumnValue("UPD_time", "string", SystemTime);
					
					ddscDs.addDataTable(ddscDtm);
				}
				else
				{
					ddscDtp.addNewRow();
					ddscDtp.setColumnValue("action", "string", "upd");
					ddscDtp.setColumnValue("VENDOR_NO", "string", this.txtVENDOR_NO.Text);
					ddscDtp.setColumnValue("VENDOR_PWD", "string", PASSWORD);
					ddscDtp.setColumnValue("status", "string", "0");
					ddscDtp.setColumnValue("UPD_id", "string", this.txtVENDOR_NO.Text);
					ddscDtp.setColumnValue("UPD_date", "string", SystemDate);
					ddscDtp.setColumnValue("UPD_time", "string", SystemTime);
					ddscDtp.setWhereValue("VENDOR_NO", this.txtVENDOR_NO.Text);
					ddscDs.addDataTable(ddscDtp);
				}
				
				
				string E_MAIL = "";
				string VENDOR_NAME = "";
				string TEXT;
				string SUBJECT = "";
				string SelectCommand = "";
				if (string.IsNullOrEmpty(VENDOR.PW_STATUS))
				{
					//SelectCommand = string.Format("select vendor_type, decode(vendor_type,\'D\',nvl(vendor_name_chinese,b2b_vendor_name_chinese),nvl(vendor_name_english,b2b_vendor_name_chinese))vendor_name from  V_VND_VENDOR WHERE PK_VENDOR_NO= \'{0}\'", this.txtVENDOR_NO.Text);
					SelectCommand = "select vendor_type, decode(vendor_type,\'D\',nvl(vendor_name_chinese,b2b_vendor_name_chinese),nvl(vendor_name_english,b2b_vendor_name_chinese))vendor_name from  V_VND_VENDOR WHERE PK_VENDOR_NO= VENDORNO";
				}
				else
				{
					//SelectCommand = string.Format("select b2b_vendor_type vendor_type, decode(b2b_vendor_type,\'D\',nvl(b2b_vendor_name_chinese,vendor_name_chinese),nvl(b2b_vendor_name_english,vendor_name_english)) vendor_name from  V_VND_VENDOR WHERE PK_VENDOR_NO= \'{0}\'", this.txtVENDOR_NO.Text);
					SelectCommand = "select b2b_vendor_type vendor_type, decode(b2b_vendor_type,\'D\',nvl(b2b_vendor_name_chinese,vendor_name_chinese),nvl(b2b_vendor_name_english,vendor_name_english)) vendor_name from  V_VND_VENDOR WHERE PK_VENDOR_NO= :VENDORNO";
					
				}
				DataSet ds = default(DataSet);
				string VENDOR_TYPE = "";
				string CONFIG_KEY = "";
				string BODY = "";
				//ds = DataBase.Query_CLASS(SelectCommand, "V_VND_VENDOR");
                m_arrParam.Add(dba.GetOracleParam(":VENDORNO", this.txtVENDOR_NO.Text));
                ds = dba.runExecuteQuery("V_VND_VENDOR", SelectCommand, m_arrParam);
				int Row = 0;
				if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
				{
					Row = System.Convert.ToInt32(ds.Tables["V_VND_VENDOR"].Rows.Count - 1);
					VENDOR_TYPE = System.Convert.ToString(ds.Tables["V_VND_VENDOR"].Rows[Row]["VENDOR_TYPE"].ToString());
					VENDOR_NAME = System.Convert.ToString(ds.Tables["V_VND_VENDOR"].Rows[Row]["VENDOR_NAME"].ToString());
				}
				if (VENDOR.E_MAIL1 != "")
				{
					E_MAIL = VENDOR.E_MAIL1 + ";";
				}
				if (VENDOR.E_MAIL2 != "")
				{
					E_MAIL += VENDOR.E_MAIL2 + ";";
				}
				if (VENDOR.E_MAIL3 != "")
				{
					E_MAIL += VENDOR.E_MAIL3 + ";";
				}
				if (VENDOR.B2BE_MAIL1 != "")
				{
					E_MAIL += VENDOR.B2BE_MAIL1 + ";";
				}
				if (VENDOR.B2BE_MAIL2 != "")
				{
					E_MAIL += VENDOR.B2BE_MAIL2 + ";";
				}
				if (VENDOR.B2BE_MAIL3 != "")
				{
					E_MAIL += VENDOR.B2BE_MAIL3 + ";";
				}
				
				if (VENDOR_TYPE == "D")
				{
					CONFIG_KEY = "PW_C";
				}
				else
				{
					CONFIG_KEY = "PW_E";
				}
                //SelectCommand = string.Format("SELECT M.CONFIG_TYPE,D.CONFIG_CHINESE_DESC,D.CONFIG_ENGLISH_DESC,D.CONFIG_VALUE FROM BAS_REFERENCE_DATA_MASTER M,BAS_REFERENCE_DATA_DETAILS D where M.CONFIG_TYPE=D.CONFIG_TYPE(+) and M.CONFIG_TYPE=\'B2B_VNDR\' and D.CONFIG_KEY =\'" + CONFIG_KEY + "\'");
                SelectCommand = "SELECT M.CONFIG_TYPE,D.CONFIG_CHINESE_DESC,D.CONFIG_ENGLISH_DESC,D.CONFIG_VALUE FROM BAS_REFERENCE_DATA_MASTER M,BAS_REFERENCE_DATA_DETAILS D where M.CONFIG_TYPE=D.CONFIG_TYPE(+) and M.CONFIG_TYPE=\'B2B_VNDR\' and D.CONFIG_KEY =:CONFIGKEY";

                m_arrParam.Clear();
                m_arrParam.Add(dba.GetOracleParam(":CONFIGKEY", CONFIG_KEY));
                ds = dba.runExecuteQuery("BAS_REFERENCE_DATA_MASTER", SelectCommand, m_arrParam);
                //ds = DataBase.Query_CLASS(SelectCommand, "BAS_REFERENCE_DATA_MASTER");
				if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
				{
					for (Row = 0; Row <= ds.Tables["BAS_REFERENCE_DATA_MASTER"].Rows.Count - 1; Row++)
					{
						SUBJECT = System.Convert.ToString(ds.Tables["BAS_REFERENCE_DATA_MASTER"].Rows[Row]["CONFIG_VALUE"].ToString().Replace("XXXX", " " + VENDOR_NAME + " "));
						
						if (VENDOR_TYPE == "D")
						{
							BODY = System.Convert.ToString(ds.Tables["BAS_REFERENCE_DATA_MASTER"].Rows[Row]["CONFIG_CHINESE_DESC"].ToString().Replace("XXXX", " " + VENDOR_NAME + " ").Replace("廠商編號:", "廠商編號:" + this.txtVENDOR_NO.Text.ToString()).Replace("登入密碼:", "登入密碼:" + PW));
						}
						else
						{
							BODY = System.Convert.ToString(ds.Tables["BAS_REFERENCE_DATA_MASTER"].Rows[Row]["CONFIG_ENGLISH_DESC"].ToString().Replace("XXXX", " " + VENDOR_NAME + " ").Replace("Account:", "Account: " + this.txtVENDOR_NO.Text.ToString()).Replace("Login Password:", "Login Password: " + PW));
						}
					}
				}
				
				
				FunctionHandler.SentMail(SUBJECT, E_MAIL, "", "", "EVAAIR Procurement Information <B2B@evaair.com>", BODY);
				
				
				dba.runExecuteNonQuery(sb.ToString());
				
				//dsResult = Update(ddscDs)
				dsResult = DataBase.Addition(ddscDs, "", (System.Array) null, (System.Array) null);
				regScript_Query(dsResult, "upd");
				
				
				
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
		}
		
		
		public void regScript_Query(DataSet ds, string action, string msg = "")
		{
			//  Dim OnUser As Employee = LoginUser()
			string strResult = System.Convert.ToString(ds.Tables[0].Rows[0]["TransResult"].ToString());
			string strMessage = System.Convert.ToString(ds.Tables[0].Rows[0]["Message"].ToString());
			// Dim OnUser As VENDOR = GetLoginUser()
			string strSuccess = "";
			string strFail = "";
			string strClose = "";
			try
			{
				if (Session["langauage"] == "zh-tw")
				{
					strSuccess = "已發MAIL通知!";
					strFail = "查詢失敗!\\n";
				}
				else
				{
					strSuccess = "QUERY SUCCESS !";
					strFail = "QUERY FAIL !\\n";
				}
				
				// 判斷交易結果
				if (strResult == "T")
				{
					if (!this.IsStartupScriptRegistered("Result"))
					{
						this.RegisterStartupScript("Result", "<script language=\"javascript\"> alert(\'" + msg + strSuccess + "\');window.navigate(\"../default.aspx\")" + strClose + " </script>");
						
					}
				}
				else if (strResult == "")
				{
					if (!this.IsStartupScriptRegistered("Result"))
					{
						this.RegisterStartupScript("Result", "<script language=\"javascript\">window.returnValue = false;</script>");
					}
				}
				else if (strResult == "F")
				{
					if (!this.IsStartupScriptRegistered("Result"))
					{
						this.RegisterStartupScript("Result", "<script language=\"javascript\"> alert(\'" + msg + strFail + strMessage + "\');window.returnValue = false; </script>");
					}
				}
			}
			catch (Exception ex)
			{
				InnerScript(ex.Message);
			}
			finally
			{
			}
		}
		
		private DataTable createErrorTable(string mError)
		{
			
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}
		
		
		
		private void btnQuery_Click(System.Object sender, System.EventArgs e)
		{
			VENDOR = CreateVENDOR(this.txtVENDOR_NO.Text);
			ResourceManager rm = BasePage.GetResourceManager();
			try
			{
				if (VENDOR.IsAutouth)
				{
					if (VENDOR.B2BE_MAIL1 == "" && VENDOR.B2BE_MAIL2 == "" && VENDOR.B2BE_MAIL3 == "")
					{
						if (VENDOR.E_MAIL1 == "" && VENDOR.E_MAIL2 == "" && VENDOR.E_MAIL3 == "")
						{
							showException(rm.GetString("MSG050"));
						}
						else
						{
							if (this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL1.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL2.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL3.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL1.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL2.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL3.ToString().ToUpper())
							{
								Query_PW();
							}
							else
							{
								showException(rm.GetString("MSG049"));
							}
						}
					}
					else
					{
						if (this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL1.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL2.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.E_MAIL3.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL1.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL2.ToString().ToUpper() || this.txtE_MAIL.Text.ToUpper() == VENDOR.B2BE_MAIL3.ToString().ToUpper())
						{
							Query_PW();
						}
						else
						{
							showException(rm.GetString("MSG049"));
						}
					}
					
				}
				else
				{
					showException(rm.GetString("MSG015"));
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
				VENDOR = null;
			}
		}
	}
	
}
